<?php
class CmsBase extends Controller {
	protected $_layout = null;
	protected $_popup_layout = null;
	protected $_require_login = false;
	protected $_level = 1;
	protected $_nav_id = 0;
	var $_lang_support = array();
	var $_lang_current = null;

	var $_settings;
	var $main_content = '';
	var $javascript = '';
	var $css = '';
	var $controller_name;
	var $skip_layout;
	var $controller_js_path;
	var $module_name = null;
	var $_assigned_to_js = '';
	var $c_lang;
	var $s_cross_lang = TRUE;

	function __construct() {
		parent :: Controller();
		$this->_layout = "layout/layout.php";
		$this->_popup_layout = "layout/popup.php";
		if ($this->controller_name == '') {
			$this->controller_name = strtolower(get_class($this));
		}		
		//load model
		$this->load->model('Model_sys', 'sys_model');

		if ($this->_require_login) {
			if (!is_user_login()) {
				redirect('cms_authentication/login');
			}
		}
		// util js
		//$this->_js_assign('fck_editor_url', public_base_url() . 'js/fckeditor/');
		//$this->_js_assign('site_url', site_url());
		// Setup CKEditor
		$this->load->library('ckeditor', base_url() . 'system/plugins/ckeditor/');
        $this->ckeditor->basePath = base_url(). 'system/plugins/ckeditor/';
        $this->ckeditor->ToolbarSet = 'Basic';
		// Setup CKFinder
        $this->load->library('ckfinder', base_url() . 'system/plugins/ckfinder/');
		$this->ckfinder->basePath = base_url(). 'system/plugins/ckfinder/';
		$this->ckfinder->SetupCKEditor($this->ckeditor, '/system/plugins/ckfinder/');
	}

	/**
	 *
	 */
	function _render($return = false) {		
		$this->_lang_current = $_SESSION[LANGUAGE_CURRENT];
		
		if ($this->skip_layout) {
			return $this->_skip_layout_render($return);
		}
		$system_top_content = '';
		if ($this->_require_login) {
			$system_top_content = $this->_system_top();
		}
		return $this->load->view($this->_layout, array (
			'content' => $this->main_content,
			'top_content' => $system_top_content,
			'javascript' => $this->javascript,
			'css' => $this->css,
			'setting' => $this->_settings,
			'lang_support' => $this->_lang_support,
			'lang_current' => $this->_lang_current,
			'assigned_to_js' => $this->_assigned_to_js), $return);
	}
	/**
	 *
	 *
	 * @return
	 */
	function _render_my_view($data) {
		$this->_lang_current = $_SESSION[LANGUAGE_CURRENT];
		
		$this->main_content = $this->_load_my_view($data);
		return $this->_render();
	}
	/**
	 *
	 *
	 * @return
	 */
	function _skip_layout_render($return) {
		$this->_lang_current = $_SESSION[LANGUAGE_CURRENT];
		
		return $this->load->view($this->_popup_layout, array (
			'content' => $this->main_content,
			'javascript' => $this->javascript), $return);

	}
	/**
	 *
	 *
	 * @return
	 */
	function _skip_layout_render_my_view($data) {
		$this->_lang_current = $_SESSION[LANGUAGE_CURRENT];
		
		$this->main_content = $this->_load_my_view($data);
		$this->_skip_layout_render();
	}

	/**
	 *
	 *
	 */
	function _system_top() {
		$this->load->model('Model_nav', 'model_nav');
		$root_navs = $this->model_nav->findTree($this->_level, $this->_nav_id, $this->_nav_id);
		arsort($root_navs, SORT_NUMERIC);
		return $this->load->view('system_top', array ('root_navs' => $root_navs), true);
	}

	/**
	 *
	 *
	 */
	function _load_javascript($name) {
		$ext = '.js';
		$js_tag = js_tag($name . $ext);
		return $js_tag;
	}

	function _mereg($array) {
		$tmp_array = $this->_get_common_params();
		if (empty ($array)) {
			return $tmp_array;
		}
		return array_merge($tmp_array, $array);
	}

	function _get_common_params() {
		$tmp_array = array ('userinfor' => $_SESSION[ADMINUSERLOGIN],
			'image_path' => image_url(), 'cms_url' => site_url(),
			'module_name' => $this->module_name, 'controller_name' => strtolower($this->router->fetch_class()),
			'method_name' => $this->router->fetch_method(), 'c_lang' => $this->c_lang,);
		return $tmp_array;
	}

	function _load_my_controller_js() {
		if (!empty ($this->controller_js_path)) {
			return $this->_load_javascript($this->controller_js_path);
		}
		if (!empty ($this->module_name)) {
			return $this->_load_my_module_js('main');
		}
	}

	function _load_my_module_js($name) {
		return $this->_load_javascript($name, $this->module_name);
	}

	function _load_my_module_view($view, $data) {
		if (!empty ($this->module_name)) {
			$view_content = $this->load->view($this->module_name . '/' . $this->controller_name . '/' . $view, $this->_mereg($data), true);
		} else {
			$view_content = $this->load->view($this->controller_name . '/' . $view, $this->_mereg($data), true);
		}
		return $view_content;
	}

	// load method view
	function _load_my_view($data) {
		$method_name = $this->router->fetch_method();
		$view_content = $this->_load_my_module_view($method_name, $data);
		return $view_content;
	}

	function _js_assign($var, $value = null) {
		if (is_array($var)) {
			foreach ($var as $k => $v) {
				$this->_assigned_to_js .= "App.data.{$k}=" . json_encode($v) . "; \n";
			} // foreach
		} else {
			$this->_assigned_to_js .= "App.data.{$var}=" . json_encode($value) . "; \n";
		} // if
	}

	/**
	 *
	 *
	 */
	function _check_permission($name, $perm = 0){
		// Get user login information
		$user = $_SESSION[ADMINUSERLOGIN];
		if(isset($user[$name]) and $user[$name]){
			return true;
		}
		else{
			// Check permisstion to access this module: wrong to access deny
			show_error('You don\'t have permission to access this page!' , 400);
		}
	}
}
?>
